package com.example.workmanager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.work.OneTimeWorkRequest;
import androidx.work.Operation;
import androidx.work.WorkContinuation;
import androidx.work.WorkManager;

import android.os.Bundle;
import android.view.View;

import com.example.workmanager.work.AWork;
import com.example.workmanager.work.BWork;
import com.example.workmanager.work.CWork;
import com.example.workmanager.work.DWork;
import com.example.workmanager.work.EWork;

import java.util.ArrayList;
import java.util.List;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    //任务链和任务组合
    public void mAddWork(View view) {


        OneTimeWorkRequest workA = new OneTimeWorkRequest.Builder(AWork.class)
                .build();

        OneTimeWorkRequest workB = new OneTimeWorkRequest.Builder(BWork.class)
                .build();
        OneTimeWorkRequest workC = new OneTimeWorkRequest.Builder(CWork.class)
                .build();
        OneTimeWorkRequest workD = new OneTimeWorkRequest.Builder(DWork.class)
                .build();
        OneTimeWorkRequest workE = new OneTimeWorkRequest.Builder(EWork.class)
                .build();

        //任务组合
        WorkContinuation continuation1 = WorkManager.getInstance(this)
                .beginWith(workA)
                .then(workB);

        WorkContinuation continuation2 = WorkManager.getInstance(this)
                .beginWith(workC)
                .then(workD);

        //将两个任务组合加入队列中
        List<WorkContinuation> list = new ArrayList<>();
        list.add(continuation1);
        list.add(continuation2);

        //最后将队列和workE压入任务队列中
        WorkContinuation.combine(list)
                .then(workE)
                .enqueue();


        //任务链 先执行A再执行B
        /*WorkManager.getInstance(this)
                .beginWith(workA)
                .then(workB)
                .enqueue();*/
    }

}